package com.itheima.health.controller;

import com.github.pagehelper.util.StringUtil;
import com.itheima.health.common.BaseContext;
import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import com.itheima.health.util.SMSUtils;
import com.itheima.health.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author ：贝
 * @date ：Created in 2022年06月07日
 * @description ：
 * 手机端密码登录
 * @version: 1.0
 */
@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
public class ValidateCodeController {

    @Autowired
    private RedisTemplate redisTemplate;

    //发送验证码
    @PostMapping("/send")
    public Result send(String type,String telephone){

        if (StringUtil.isNotEmpty(telephone)){

            //生成4位数眼中验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("我们要发送给用户的验证码是:"+code);
            //调用阿里云进行短信发送1
            //SMSUtils.sendMessage("传智健康","",telephone,code);
            //存到redis里
            // 我们要把手机号 和验证码 存储到 redis中  键 字符串  值 字符串
            String key = RedisConstant.VALIDATE_CODE_PREFIX +"_"+type+"_"+telephone;
            //这种key的写法是遵循一种开发默认规则 不同公司规则不同

            redisTemplate.opsForValue().set(key,code,2, TimeUnit.MINUTES);

            return new Result(true,MessageConst.SEND_VALIDATECODE_SUCCESS);

        }
        return new Result(false, MessageConst.SEND_VALIDATECODE_FAIL);
    }




}
